package com.mercenary.wein.screenControl.ui.batch;

import com.mercenary.wein.screenControl.deviceControll.AndroidDevice;
import com.mercenary.wein.screenControl.ui.DeviceTable;
import com.mercenary.wein.screenControl.ui.MainFrame;
import com.mercenary.wein.screenControl.ui.SimpleScreenPanel;
import com.mercenary.wein.screenControl.ui.TwoInputDialog;
import com.mercenary.wein.screenControl.ui.inter.DeviceTableOb;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.Border;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;


public class MessageList extends JDialog {
    JPanel J1 = new JPanel();
    MyPanel J2 = new MyPanel();
    JPanel J3 = new JPanel();
    DeviceTable J4 = new DeviceTable();
    static JFrame jf = new JFrame();

    public MessageList() {
        super(jf, "集中聊天");
        setBounds(30, 100, 1000, 600);
        setLayout(new BorderLayout());
        setVisible(true);
        add(J1, BorderLayout.WEST);
        add(J2, BorderLayout.CENTER);
        add(J3, BorderLayout.EAST);
        J1.setPreferredSize(new Dimension(400, 600));//设置大小
        J1.setBackground(Color.WHITE);
        J1.add(J4);//在第一块面板上添加一个滚动面板，存放连接手机列表
        J4.setPreferredSize(new Dimension(400, 600));
        J2.setPreferredSize(new Dimension(270, 600));
        J2.setBackground(Color.WHITE);
        J3.setPreferredSize(new Dimension(250, 600));//设置大小
        J3.setBackground(Color.WHITE);
        Border titleBorder1 = BorderFactory.createTitledBorder("手机列表");// 设置面板边框，实现分组框的效果       .设置面板边框颜色
        J1.setBorder(titleBorder1);
        Border titleBorder2 = BorderFactory.createTitledBorder("聊天窗口");
        J2.setBorder(titleBorder2);
        Border titleBorder3 = BorderFactory.createTitledBorder("快捷回复");
        J3.setBorder(titleBorder3);
        //J1中列表的组

        J1.validate();
        //J2组件
        //AndroidDevice androidDevice=new AndroidDevice(null);
        // androidDevice.getDevice().createForward(deviceBean.getPort(),3321);
        //J3组件
        J3.setLayout(new BorderLayout());
        Vector<String> columnNames1 = new Vector<String>();
        columnNames1.add("文案信息库");
        JScrollPane devicetable = new JScrollPane();
        JPanel com1 = new JPanel(new FlowLayout(FlowLayout.LEADING));
        Vector<List<String>> messageList = new Vector<List<String>>();

        DefaultTableModel tm = new DefaultTableModel( messageList,columnNames1);
        JTable table = new JTable(tm);
        DefaultTableCellRenderer tableCellRenderer = new DefaultTableCellRenderer();// 设置table内容居中
        table.setDefaultRenderer(Object.class, tableCellRenderer);
        table.setPreferredScrollableViewportSize(new Dimension(100, 20));
        devicetable .setViewportView(table);

        JComboBox comboBox = new JComboBox();
        List<String> combos = new ArrayList<String>();
        combos.add("全部消息");

        for (int i = 0; i < combos.size(); i++) {
            comboBox.addItem(combos.get(i));

        }
        JButton B1 = new JButton("message");

        B1.setPreferredSize(new Dimension(70, 20));
        com1.add(B1);
        com1.add(comboBox);

        J3.add(devicetable, BorderLayout.NORTH);
        J3.add(com1, BorderLayout.SOUTH);//手机列表
        J3.validate();
        B1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //新建分组的弹出框
                //String inputValue = JOptionPane.showInputDialog("请输入分组名称");
                TwoInputDialog inputdialog = new TwoInputDialog(null);
                // inputdialog.registerGroupComboBox(comboBox);
                J3.validate();
                //tabPanel.setVisible(true);
            }
        });
        J4.refreshData(0);
        J4.stopListen();
        J4.registerObserver(J2);


    }

    public static void main(String[] arge) {
        new MessageList();
    }
}

class MyPanel extends JPanel implements DeviceTableOb{
    JScrollPane jsp = new JScrollPane();
    SimpleScreenPanel ssp;
    AndroidDevice androidDevice ;
    public MyPanel(){
        super();
    }
    @Override
    public void refreshData(String serialNo) {
        int i = this.getComponentCount();
        while(i>0){
            this.remove(0);
            i--;
        }
        if(androidDevice!=null&&ssp!=null)androidDevice.getScreenCaptureThread().removeListener(ssp);
        androidDevice = MainFrame.dcdMap.get(serialNo).getAndroidDevice();
        ssp = new SimpleScreenPanel();
        androidDevice.getScreenCaptureThread().setListener(ssp);
        jsp.setViewportView(ssp);
        jsp.setPreferredSize(new Dimension(this.getWidth(),this.getHeight()));
        this.add(jsp);
        jsp.updateUI();
        updateUI();
    }
}